﻿namespace MonetDbDataProvider
{
    using System;
    using System.Linq;

    public class ConnectionString
    {
        public ConnectionString(string connectionString)
        {
            if (string.IsNullOrEmpty(connectionString))
                throw new ArgumentException("A connection string must be provided.");

            var connectionParams = connectionString.Split(';');

            foreach (var param in connectionParams.Select(connectionParam => connectionParam.Split('=')))
            {
                switch (param[0].ToLower())
                {
                    case "host":
                        this.Host = param[1];
                        break;
                    case "port":
                        this.Port = int.Parse(param[1]);
                        break;
                    case "database":
                        this.Database = param[1];
                        break;
                    case "username":
                        this.Username = param[1];
                        break;
                    case "password":
                        this.Password = param[1];
                        break;
                }
            }
        }

        public string Host { get; set; }

        public int Port { get; set; }

        public string Database { get; set; }

        public string Username { get; set; }

        public string Password { get; set; }

        public override string ToString()
        {
            return string.Format("database={0};host={1};port={2};username={3};password={4};", this.Database, this.Host, this.Port, this.Username, this.Password);
        }
    }
}
